package databasetest;

import java.awt.EventQueue;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

import javax.swing.JInternalFrame;
import javax.swing.JTextField;
import javax.swing.JLabel;
import javax.swing.JButton;
import javax.xml.crypto.Data;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.event.InternalFrameAdapter;
import javax.swing.event.InternalFrameEvent;

public class Delete extends JInternalFrame {
	Connection conn;
	DataBaseTest main=null;
	private JTextField code;
	private JTextField ea;
	private JLabel label;
	private JLabel label_1;
	private JButton search;
	private JLabel result;
	private JButton button_1;
	private JLabel label_3;
	private JTextField serial;

	public Delete(Connection conn,DataBaseTest d) {
		super("삭제",true,true,true,true);
		addInternalFrameListener(new InternalFrameAdapter() {
			@Override
			public void internalFrameClosing(InternalFrameEvent arg0) {
				serial.setText("");
				code.setText("");
				ea.setText("");
			}
		});
		this.conn = conn;
		this.main = d;
		setBounds(400, 100, 450, 300);
		getContentPane().setLayout(null);
		getContentPane().add(getCode());
		getContentPane().add(getEa());
		getContentPane().add(getLabel());
		getContentPane().add(getLabel_1());
		getContentPane().add(getSearch());
		getContentPane().add(getResult());
		getContentPane().add(getButton_1());
		getContentPane().add(getLabel_3());
		getContentPane().add(getSerial());

	}
	public void setData(String s, String c, String e){
    	serial.setText(s);
    	code.setText(c);
    	ea.setText(e);
    	
    }
	private JTextField getCode() {
		if (code == null) {
			code = new JTextField();
			code.setColumns(10);
			code.setBounds(111, 85, 116, 21);
		}
		return code;
	}
	private JTextField getEa() {
		if (ea == null) {
			ea = new JTextField();
			ea.setColumns(10);
			ea.setBounds(111, 128, 116, 21);
		}
		return ea;
	}
	private JLabel getLabel() {
		if (label == null) {
			label = new JLabel("코드");
			label.setBounds(42, 88, 57, 15);
		}
		return label;
	}
	private JLabel getLabel_1() {
		if (label_1 == null) {
			label_1 = new JLabel("수량");
			label_1.setBounds(42, 131, 57, 15);
		}
		return label_1;
	}
	private JButton getSearch() {
		if (search == null) {
			search = new JButton("조회");
			search.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent arg0) {
					try{

						int r = Integer.parseInt(serial.getText()); //serial 값 가져오기!

						String sql = "select  * from protbl where serial=?";//코드의 행을 찾는다.
						PreparedStatement pstmt = conn.prepareStatement(sql);
						pstmt.setInt(1, r);    //첫번째 물음표에 serial 값


						ResultSet rs =pstmt.executeQuery();//select 된 결과를 받아서 ResultSet 타입으로 받아서 rs 저장 

						if(rs.next()){  //값이 있는지 찾는다.

							code.setText(rs.getString("code"));//찾은 값의 1번째를 코드 텍스트 필드에 넣는다.
							ea.setText(rs.getInt("ea")+""); //찾은 값의 2번째인 수량을 텍스트 필드에 넣는다.

							result.setText("serial "+serial.getText()+"인 데이터가 조회되었습니다.");
							code.setEditable(false);
							ea.setEditable(false);

						}else{
							code.setText("");
							ea.setText("");
							result.setText("조회된 데이터가 없습니다.");
						}
					}catch(Exception ex){
						ex.printStackTrace();
					}
				}
			});
			search.setBounds(278, 43, 116, 23);
		}
		return search;
	}
	private JLabel getResult() {
		if (result == null) {
			result = new JLabel("New label");
			result.setBounds(12, 225, 410, 15);
		}
		return result;
	}
	private JButton getButton_1() {
		if (button_1 == null) {
			button_1 = new JButton("삭제");
			button_1.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent arg0) {
					try{
						int r = Integer.parseInt(serial.getText());

						String sql = "delete from protbl where serial=?";
                        PreparedStatement pstmt = conn.prepareStatement(sql);
                        
                        pstmt.setInt(1,r);
                        int exe = pstmt.executeUpdate();
                        if(exe==1){
                        	result.setText("serial "+r +"의 데이터를 삭제했습니다.");
                            code.setText("");
                            ea.setText("");
                        }else{
                        	result.setText("데이터가 삭제가 되지 않았습니다.");
                        }
                      
                        if(main.select !=null) main.select.search();
					}catch(Exception ex){
						ex.printStackTrace();
					}
				}
			});
			button_1.setBounds(111, 175, 116, 23);
		}
		return button_1;
	}
	private JLabel getLabel_3() {
		if (label_3 == null) {
			label_3 = new JLabel("serial");
			label_3.setBounds(42, 47, 57, 15);
		}
		return label_3;
	}
	private JTextField getSerial() {
		if (serial == null) {
			serial = new JTextField();
			serial.setColumns(10);
			serial.setBounds(111, 44, 116, 21);
		}
		return serial;
	}
}
